﻿using DemoApplication.Config;
using EntityFrameworkLayer.Entities;
using System.Collections.Generic;
using System.Windows;

namespace DemoApplication.Dialog
{
    /// <summary>
    /// Interaction logic for SearchMaterial.xaml
    /// </summary>
    public partial class SearchMaterialWindow : Window
    {
        private Material m_Material;

        public SearchMaterialWindow()
        {
            InitializeComponent();

            m_Material = new Material();
        }

        public Material SearchMaterial
        {
            get 
            { 
                return m_Material; 
            }
            set
            { 
                m_Material.Copy(value);
                m_Material.Classify = (!string.IsNullOrEmpty(m_Material.Classify) ? m_Material.Classify : "No filter");
                m_Material.CostUnit = (!string.IsNullOrEmpty(m_Material.CostUnit) ? m_Material.CostUnit : "No filter");
                m_Material.QuantityUnit = (!string.IsNullOrEmpty(m_Material.QuantityUnit) ? m_Material.QuantityUnit : "No filter");
            }
        }

        #region event handling functions

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            StandardizeSearchObject();
            this.DialogResult = true;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }   

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            cbbClassify.ItemsSource = CreateListWithNoFilter(Configuration.GetClassifyList());
            cbbCostUnit.ItemsSource = CreateListWithNoFilter(Configuration.GetCostUnitList());
            cbbQuantityUnit.ItemsSource = CreateListWithNoFilter(Configuration.GetQuantityUnitList());

            this.DataContext = m_Material;
        }

        #endregion

        #region Private functions

        private List<string> CreateListWithNoFilter(List<string> list)
        {
            List<string> result = new List<string>(list);
            result.Insert(0, "No filter");
            return result;
        }

        private void StandardizeSearchObject()
        {
            m_Material.Classify = ("No filter" != m_Material.Classify ? m_Material.Classify : "");
            m_Material.CostUnit = ("No filter" != m_Material.CostUnit ? m_Material.CostUnit : "");
            m_Material.QuantityUnit = ("No filter" != m_Material.QuantityUnit ? m_Material.QuantityUnit : ""); 
        }

        #endregion
    }
}
